﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestSystem.DataModel;
using System.IO;

namespace TestSystem.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }

        String selected = String.Empty;


        private String GetUserType()
        {
            return BindingSource.GetInstance().StudentTag;
        }

        protected void CreateUserButton_Click(Object sender, EventArgs e)
        {
            String filePath = BindingSource.GetInstance().UserFilePath;

            if (File.Exists(filePath))
            {
                using (StreamWriter streamWriter = new StreamWriter (filePath, true))
                {

                    DataModel.Account account = new DataModel.Account (this.RegisterUser.UserName, 
                                                                        this.RegisterUser.Password,
                                                                        (BindingSource.GetInstance().TotalAccounts++).ToString(),
                                                                        GetUserType());

                    streamWriter.WriteLine(account.ToString());
                }
            }

            Response.Redirect("Login.aspx");
        }
    }
}
